#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>


%hook NSURL

+ (id)URLWithString:(NSString *)URLString
{
    if (!URLString || URLString.length == 0) {
        return %orig; // 防止空字符串导致崩溃
    }

    static NSArray *blockedURLPatterns = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        blockedURLPatterns = @[
            // Grindr 广告 SDK 域名
            @"^https://[a-zA-Z0-9.-]+\\.applovin\\.com",        // AppLovin
            @"^https://[a-zA-Z0-9.-]+\\.applvn\\.com",          // AppLovin 备用域名
            @"^https://[a-zA-Z0-9.-]+\\.mintegral\\.net",       // Mintegral
            @"^https://[a-zA-Z0-9.-]+\\.inner-active\\.mobi",   // InnerActive
            @"^https://[a-zA-Z0-9.-]+\\.doubleclick-cn\\.net",  // Google DoubleClick (中国)
            @"^https://[a-zA-Z0-9.-]+\\.unitychina\\.cn",       // Unity Ads (中国)
            @"^https://[a-zA-Z0-9.-]+\\.inmobi\\.com",          // InMobi
            @"^https://[a-zA-Z0-9.-]+\\.unity3d\\.com",         // Unity Ads
            @"^https://[a-zA-Z0-9.-]+\\.braze\\.com",           // Braze (用户分析)
            @"^https://[a-zA-Z0-9.-]+\\.vungle\\.com",           // Vungle
        ];
    });

    for (NSString *pattern in blockedURLPatterns) {
        NSError *error = nil;
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
        if (!error) {
            NSUInteger matches = [regex numberOfMatchesInString:URLString options:0 range:NSMakeRange(0, URLString.length)];
            if (matches > 0) {
                URLString = @"https://127.0.0.1"; // 替换为无效但安全的 URL
                break;
            }
        }
    }

    return %orig(URLString);
}

%end


%hook UIView

- (id)initWithFrame:(CGRect)frame {
    self = %orig;
    NSString *className = NSStringFromClass([self class]);
    
    NSArray *hiddenClasses = @[
        @"ServerDrivenCascadeAdCell",
        @"_TtGC7SwiftUI14_UIHostingViewGV7grindrx21FloatingStatusBarViewVS_7AnyView__",
        @"ServerDrivenCascadeExploreLocationsCell",
        @"UpsellBannerXtraCell",
        @"UpsellBannerBoostCell",
        @"ServerDrivenCascadeTopPicksCell",
        @"AdView"
    ];
    
    BOOL shouldHide = NO;
    for (NSString *hiddenClass in hiddenClasses) {
        if ([className containsString:hiddenClass]) {
            shouldHide = YES;
            break;
        }
    }
    
    if (shouldHide && ![className isEqualToString:@"RewarderAdViewedMeProfileCell"]) {
        self.hidden = YES;
        self.alpha = 0;
        self.userInteractionEnabled = NO;
    }
    return self;
}

%end

